package frame;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

@Configuration // 配置类
@ConfigurationProperties(prefix = "frame.app")
@WebListener
public class AppConfiguration implements ServletContextListener {

    /** 系统名称+版本*/
    private String systemInfo;
    /** javascript 版本 用于客户端缓存清理*/
    private String jsVersion;

    private ServletContext servletContext;

    @Bean("df1") // 定义额外的bean
    public DateFormat getDateFormat() {
        return new SimpleDateFormat("HH:mm:ss");
    }

    @Bean("df2") // 定义额外的bean
    public DateFormat getDateFormat2() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    }

    // @Qualifier 指定方法参数使用哪个bean
    @Bean()
    public String startTime(@Qualifier("df2") DateFormat df) {
        return df.format(new Date());
    }


    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        configSource.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsFilter(configSource);
    }
    @Bean
    public HttpMessageConverters fastJsonHttpMessageConverters() {
        //1、定义一个convert转换消息的对象
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
        //2、添加fastjson的配置信息
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(
                SerializerFeature.PrettyFormat,//
                SerializerFeature.WriteMapNullValue,//空Map = []
                SerializerFeature.WriteNullListAsEmpty,// List = []
                SerializerFeature.DisableCircularReferenceDetect//禁止循环引用
        );
        //3、在convert中添加配置信息
        fastConverter.setFastJsonConfig(fastJsonConfig);
        //4、将convert添加到converters中
        HttpMessageConverter<?> converter = fastConverter;
        return new HttpMessageConverters(converter);
    }


    @Override
    public void contextInitialized(ServletContextEvent sce) {
        servletContext = sce.getServletContext();
        servletContext.setAttribute("js_version",jsVersion);
        servletContext.setAttribute("system_info",systemInfo);
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {

    }

    public String getJsVersion() {
        return jsVersion==null?"1.0":jsVersion;
    }

    public void setJsVersion(String jsVersion) {
        this.jsVersion = jsVersion;
    }

    public String getSystemInfo() {
        return systemInfo;
    }

    public void setSystemInfo(String systemInfo) {
        this.systemInfo = systemInfo;
    }
}
